package com.jsbj.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jsbj.tools.RequestHeaderUtils;
import com.jsbj.tools.StringUtils;
import com.jsbj.user.entities.SysRoleEqu;
import com.jsbj.user.mapper.SysRoleEquMapper;
import com.jsbj.user.service.ISysRoleEquService;
import org.apache.commons.collections.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 角色-设备信息表 服务实现类
 * </p>
 *
 * @author jsbj
 * @since 2022-03-07
 */
@Service
public class SysRoleEquServiceImpl extends ServiceImpl<SysRoleEquMapper, SysRoleEqu> implements ISysRoleEquService {

    @Autowired
    private HttpServletRequest request;

    @Override
    public Integer add(Map<String,Object> inParam) {
        String userId = RequestHeaderUtils.getUserId(request);
//        JSONObject object = JSONObject.parseObject(inParam);
        String equIds = MapUtils.getString(inParam,"equIds");
        String roleId = MapUtils.getString(inParam,"roleId");
        //先删除再保存
        baseMapper.delete(Wrappers.<SysRoleEqu>lambdaUpdate()
                .eq(SysRoleEqu::getRoleId,roleId));
        if (StringUtils.isNotEmpty(equIds)) {
            List<String> deviceIds = Arrays.asList(equIds.split(","));
            for (String equId : deviceIds) {
                SysRoleEqu sysRoleEqu = new SysRoleEqu();
                sysRoleEqu.setEquId(equId);
                sysRoleEqu.setRoleId(roleId);
                sysRoleEqu.setCreaterId(userId);
                sysRoleEqu.setCreateTime(new Date());
                sysRoleEqu.setUpdateId(userId);
                sysRoleEqu.setUpdateTime(new Date());
                baseMapper.insert(sysRoleEqu);
            }
        }

        return 1;
    }

    @Override
    public List<SysRoleEqu> detail(String id) {
        LambdaQueryWrapper<SysRoleEqu> qw = new LambdaQueryWrapper<>();
        qw.eq(SysRoleEqu::getRoleId,id);
        qw.eq(SysRoleEqu::getDisabled,0);
        List<SysRoleEqu> sysRoleEqus = baseMapper.selectList(qw);
        return sysRoleEqus;
    }
}
